import axios from 'axios'
import commonUrl from '../common/js/commonUrl'
import { loadUserId } from '../common/js/cache'
import qs from 'qs'

// 我的查验页面部分
// 接单人员认证
export function takeOrderIdent (username, phone, cardId, typeId, serverIds) {
  const url = `${commonUrl.apihost}index.php/home/Identify/personAuth`
  const data = {
    uid: loadUserId(),
    username: username,
    phone: phone,
    id_card: cardId,
    category_id: typeId,
    serverIds: serverIds
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 发单人员认证
export function billIdent (username, phone, idCard, serverIds, cname, cnumber, address) {
  const url = `${commonUrl.apihost}index.php/home/Identify/publishAuth`
  const data = {
    uid: loadUserId(),
    username: username,
    phone: phone,
    id_card: idCard,
    serverIds: serverIds,
    company_name: cname,
    company_number: cnumber,
    address: address
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 获取认证状态
export function identStatus (types) {
  const url = `${commonUrl.apihost}index.php/home/Identify/identifyStatus`
  const data = {
    uid: loadUserId(),
    type: types
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 接单平台页面部分
// 发布接单
export function issueOrder (areasId, orderTitle, describe, categoryId, time) {
  const url = `${commonUrl.apihost}index.php/home/examinework/releasework`
  const data = {
    uid: loadUserId(),
    areas_id: areasId,
    title: orderTitle,
    describe: describe,
    category_id: categoryId,
    end_time: time
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 接单平台列表
export function takeOrderList (areaid, sortid) {
  const url = `${commonUrl.apihost}index.php/home/examinework/workList`
  const data = {
    district_id: areaid,
    category_id: sortid,
    uid: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 接单平台的默认列表
export function takeOrderDefault () {
  const url = `${commonUrl.apihost}index.php/home/examinework/defaultList`
  const data = {
    uid: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 接单平台列表详情
export function orderListDetail (id) {
  const url = `${commonUrl.apihost}index.php/home/examinework/workDetail`
  const data = {
    work_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 接单平台列表详情页面的我要接单
export function pickOrder (id) {
  const url = `${commonUrl.apihost}index.php/home/examinework/takingwork`
  const data = {
    uid: loadUserId(),
    work_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 发单订单部分接口
// 已发单列表
export function billList () {
  const url = `${commonUrl.apihost}index.php/home/examinework/myExamineWork`
  const data = {
    uid: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 已发单列表的编辑
export function billEdit (id, title, des, time) {
  const url = `${commonUrl.apihost}index.php/home/examinework/examineWorkEdit`
  const data = {
    uid: loadUserId(),
    work_id: id,
    title: title,
    describe: des,
    end_time: time
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 待确认列表
export function toBeFinishList (types, status) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderlist`
  const data = {
    uid: loadUserId(),
    type: types,
    t_status: status
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 待确认列表的拒绝接单
export function refuseOrder (id, num) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderRequest`
  const data = {
    uid: loadUserId(),
    order_id: id,
    status: num
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 待确认列表的同意接单
export function agreeOrder (id, num) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderRequest`
  const data = {
    uid: loadUserId(),
    order_id: id,
    status: num
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 待确认列表的确认完成
export function confirmOrder (id, num) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderRequest`
  const data = {
    uid: loadUserId(),
    order_id: id,
    status: num
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 已完成列表
export function finishedList (types, status) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderlist`
  const data = {
    uid: loadUserId(),
    type: types,
    t_status: status
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 接单订单部分接口
// 待审核列表
export function auditList (types, status) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderlist`
  const data = {
    uid: loadUserId(),
    type: types,
    t_status: status
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 待审核列表的取消订单
export function cancelOrder (id, num) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderRequest`
  const data = {
    uid: loadUserId(),
    order_id: id,
    status: num
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 未完成列表
export function notFinishList (types, status) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderlist`
  const data = {
    uid: loadUserId(),
    type: types,
    t_status: status
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 未完成列表的确认完成
export function okFinish (id, num) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderRequest`
  const data = {
    uid: loadUserId(),
    order_id: id,
    status: num
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 已完成列表
export function doneList (types, status) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderlist`
  const data = {
    uid: loadUserId(),
    type: types,
    t_status: status
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 订单的详情(已发单)
export function orderDetail (id) {
  const url = `${commonUrl.apihost}index.php/home/examinework/workDetail`
  const data = {
    work_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 订单详情（除已发单外其他所有列表的详情）
export function orderDetails (id) {
  const url = `${commonUrl.apihost}index.php/home/examineorder/orderDetail`
  const data = {
    order_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
